package com.angel.web.app;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.base.JsonEntity;
import com.angel.core.util.JsonUtil;
import com.angel.health.BuzMedicineRecord;
import com.angel.provider.health.BuzMedicineRecordProvider;

/** 
* @author 作者姓名  cheng
* @version 
* 创建时间：2016年9月24日 上午11:08:40 
* 类说明   app用药记录部分
*/
@Controller
@RequestMapping("app/medicineRecord")
public class AppMedicineRecordController {
	@Autowired
	private BuzMedicineRecordProvider bmrProvider;

	/**
	 * 新增用药记录
	 * @param bmr
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addMR",produces="application/json; charset=utf-8")
	public String addMR(BuzMedicineRecord bmr){
		int insert = bmrProvider.insert(bmr);
		if(insert > 0 ){
			return JsonUtil.toJson(new JsonEntity("200", bmr));
		}
		return JsonUtil.toJson(new JsonEntity("-1", "添加失败！"));
	}
	
	/**
	 * 查看用药记录
	 * @param bmr
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="index",produces="application/json; charset=utf-8")
	public String index(BuzMedicineRecord bmr){
		bmr.setEnable(1);
		List<Map<String, Object>> list = bmrProvider.findAll(bmr);
		return JsonUtil.toJson(new JsonEntity("200", list));
	}
	
	/**
	 * 删除用药记录
	 * @param buzExamination
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "del", produces = "application/json; charset=UTF-8")
	public String del(BuzMedicineRecord bmr) {
		int delById = bmrProvider.delById(bmr);
		if (delById > 0) {
			return JsonUtil.toJson(new JsonEntity("200", bmr));
		} else {
			return JsonUtil.toJson(new JsonEntity("-1", "失败"));
		}
	}

	/**
	 * 编辑用药记录
	 * 
	 * @param buzExamination
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "edit", produces = "application/json; charset=UTF-8")
	public String editExamination(BuzMedicineRecord bmr) {
		if (bmr.getId() == 0 || bmr.getId() == null) {
			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
		}
		try {
			int updateById = bmrProvider.updateById(bmr);
			if (updateById > 0) {
				return JsonUtil.toJson(new JsonEntity("200", bmr));
			} else {
				return JsonUtil.toJson(new JsonEntity("-1", "失败"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return JsonUtil.toJson(new JsonEntity("-1", "失败"));
		}
	}
}
